<template>
  <div>
    <supertable
      class="bannertable"
      :tableData="tableData"
      :headerData="headerData"
      :isoperation="false"
      :ispagination="false"
      :height="1000"
      @Delete="DeleteFn"
      @IDchange="IDchangeFn"
    >
      <template v-slot:idFile="row">
        <div>
          <el-image :src="row.row.idFile"></el-image>
        </div>
      </template>
      <template v-slot:formSolt>
        <div>
          <el-input
            placeholder="标题"
            v-model="input"
            clearable
            class="bannerinput"
          >
          </el-input>
          <el-button
            type="success"
            size="mini"
            icon="el-icon-search"
            @click="bannerlistFn()"
            >搜索</el-button
          >
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-search"
            @click="reset()"
            >重置</el-button
          >
        </div>
      </template>
      <template v-slot:btnSolt>
        <div class="bannerBtn">
          <el-button
            type="success"
            size="mini"
            icon="el-icon-plus"
            @click="bannerAdd()"
            >添加</el-button
          >
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="bannerDeleteFn()"
            >删除</el-button
          >
        </div>
      </template>
    </supertable>
    <!-- 添加 -->
    <div>
      <el-dialog
        title="添加banner"
        :visible.sync="dialogVisible"
        width="70%"
        v-DragWindow
      >
        <span>
          <template>
            <div>
              <el-row>
                <el-col :span="12">
                  <el-col :span="6" style="text-align: right; font-weight: bold"
                    >banner图片</el-col
                  >
                  <el-col :span="18">
                    <div>
                      <el-upload
                        class="bannerUpload"
                        :multiple="false"
                        :action="uploadUrl"
                        :headers="uploadHeaders"
                        :on-success="handleUploadSuccess"
                      >
                        <!-- http-request默认行为取消 进行自定义上传 -->
                        <i class="el-icon-upload"></i>
                        <div class="bannerel-upload__text">上传图片</div>
                      </el-upload>
                    </div>
                  </el-col>
                </el-col>
                <el-col :span="12">
                  <el-form
                    :model="ruleForm"
                    :rules="rules"
                    ref="ruleForm"
                    label-width="100px"
                    class="demo-ruleForm"
                    :show-message="false"
                  >
                    <el-form-item label="标题" prop="title">
                      <el-input v-model="ruleForm.title"></el-input>
                    </el-form-item>
                  </el-form>
                  <el-form ref="form" :model="form" label-width="100px">
                    <el-form-item
                      label="打开的界面"
                      class="bannerForm"
                      prop="interface"
                    >
                      <el-input v-model="form.interface"></el-input>
                    </el-form-item>
                    <el-form-item
                      label="参数"
                      class="bannerForm"
                      prop="parameter"
                    >
                      <el-input
                        type="textarea"
                        v-model="form.parameter"
                      ></el-input>
                    </el-form-item>
                    <el-form-item label="类型" class="bannerForm" prop="type">
                      <el-select
                        v-model="form.type"
                        placeholder="请选择"
                        style="width: 100%"
                      >
                        <el-option
                          v-for="item in options"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                        >
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-form>
                </el-col>
              </el-row>
            </div>
          </template>
        </span>

        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="append()">提交</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
//引入组件
import supertable from "@/components/supertable";
import { bannerlist, banner, bannerDelete } from "@/api/BannerManagement";
import { GetCookie } from "@/config/config";
export default {
  name: "BannerManagement",
  data() {
    return {
      uploadUrl: "http://localhost:9528/dev-api/file",
      uploadFileId: "",
      uploadHeaders: {
        Authorization: GetCookie("token"),
      },
      goodsindex: "",
      input: "",
      dialogVisible: false,
      idFile: "",
      ruleForm: {
        title: "",
      },
      form: {
        interface: "",
        parameter: "",
        type: "",
      },
      options: [
        { label: "首页", value: "index" },
        { label: "新闻", value: "news" },
        { label: "产品", value: "product" },
        { label: "解决方案", value: "solution" },
        { label: "案例", value: "case" },
      ],
      headerData: [
        {
          prop: "ID",
          label: "ID",
          width: "",
        },
        {
          prop: "title",
          label: "标题",
          width: "",
        },
        {
          prop: "type",
          label: "类别",
          width: "",
        },
        {
          prop: "page",
          label: "打开界面",
          width: "",
        },
        {
          prop: "param",
          label: "参数",
          width: "",
        },
        {
          prop: "idFile",
          label: "图片",
          width: "",
          slot: true,
        },
      ],
      tableData: [],

      rules: {
        title: { required: true, trigger: "blur" },
      },
    };
  },

  async mounted() {
    let _self = this;
    await _self.bannerlistFn();
  },
  // component:{组件定义名字：该组件的路径}
  components: {
    supertable,
  },
  methods: {
    //上传图片
    // httprequest(par) {
    //   let _self = this;

    //   console.log(par);
    //   let formData = new FormData();
    //   formData.append("file", par.file);
    //   file(formData)
    //     .then((res) => {
    //       console.log(res);
    //     })
    //     .catch((err) => error);
    //   //   _self.idFile=par.file.name
    //   // console.log(_self.idFile);
    // },

    //上传
    handleUploadSuccess(response, raw) {
      console.log(response);
      if (response.code === 20000) {
        this.uploadFileId = response.data.realFileName;
        this.uploadFileId = response.data.originalFileName;
      } else {
        this.$message({
          message: this.$t("common.uploadError"),
          type: "error",
        });
      }
    },
    //删除
    DeleteFn(val) {
      console.log(val);
      let _self = this;
      _self.goodsindex = val[0].ID;
    },

    IDchangeFn(val) {
      console.log(val);
      let _self = this;
      _self.goodsindex = val.ID;
    },
    //删除
    bannerDeleteFn() {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let _self = this;
          let data = {
            id: _self.goodsindex,
          };
          bannerDelete(data).then((res) => {
            console.log(res);
            if (res.code == 20000) {
              _self.bannerlistFn();
            }
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //提交接口
    append() {
      let _self = this;
      let data = {
        id: "",
        title: _self.ruleForm.title,
        page: _self.form.interface,
        param: _self.form.parameter,
        idFile: _self.uploadFileId,
        type: _self.form.type,
      };
      data.param = encodeURIComponent(data.param);
      banner(data).then((res) => {
        console.log(res);
        if (res.code == 20000) {
          _self.bannerlistFn();
          _self.dialogVisible = false;
          _self.$refs.ruleForm.resetFields();
          _self.$refs.form.resetFields();
          this.$message({
            message:"添加成功",
            type:"success"
          })
        }
      });
    },

    //banner添加
    bannerAdd() {
      let _self = this;
      _self.dialogVisible = true;
    },
    //重置
    reset() {
      let _self = this;
      _self.input = "";
      _self.bannerlistFn();
    },

    //id title type page param idFile
    // 商品列表数据
    async bannerlistFn() {
      let _self = this;
      _self.tableData = [];
      let data = {
        title: _self.input.trim(),
      };
      await bannerlist(data).then((res) => {
        console.log(res);
        if (res.code == 20000) {
          for (let item of res.data) {
            _self.tableData.push({
              ID: item.id,
              title: item.title,
              type: item.type,
              page: item.page,
              param: item.param,
              idFile:
                "http://localhost:9528/dev-api/file/getImgStream?idFile=" +
                item.idFile,
            });
          }
        }
      });
    },
  },
};
</script>
<style scoped lang="">
.bannerinput {
  margin: 10px 10px;
  width: 400px;
}
.bannertable {
  margin: 10px;
}
.bannerBtn {
  margin: 10px;
}
.el-image {
  width: 200px;
  height: 100px;
}
.bannerUpload {
  font-size: 100px;
  border: 1px dashed rgb(206, 199, 199);
  text-align: center;
  color: #c0c4cc;
}
.bannerel-upload__text {
  font-size: 16px;
}
</style>

